Skip to content
This repository was archived by the owner on Sep 30, 2019. It is now read-only.

Conversation

@asciipip
Copy link

This includes a module for interacting with the TSL2561 light sensor. I hope that the main class's documentation is sufficient for use. If you have any questions or problems, please let me know.

@csatt
Copy link

csatt commented Dec 11, 2015

Hi Phil,

As you may know, another pull request for a Adafruit_TSL2561 module was
submitted by Iain Colledge a couple days after you submitted this
one. He announced it on this thread on the Adafruit forum:

https://forums.adafruit.com/viewtopic.php?f=8&t=34922&p=430913#p430913

His is a very direct port of the Adafruit Arduino code which uses the
integer math from the data sheet. I was looking for just such a thing,
so I downloaded his module and tested it. After fixing a couple bugs, it
works well. He mentioned your pull request, so I decided to download it
and give it a try as well.

I found (and debugged) two bugs that need to be fixed in your code:

Bug #1: Bytes are swapped in 16-bit data from CHAN0 and CHAN1

    In the _getData method, the I2C reverseByteOrder method is
    called on the values read with readU16. I've seen this in other
    versions of this code, but in my testing it clearly appears to
    be wrong. I slowly moved my light source closer and closer to
    the sensor. Without the byte swap the values smoothly increase
    in the lower byte first, and then the upper byte starts
    increasing.

Bug #2: The gain is applied incorrectly in the scaling for the lux calculation

    The following line is wrong:

    chscale = self._integrationTime.scale * self.gain

    When the gain is set to 1, the value must be multiplied by
    16. And when the gain is set to 16, the value is used
    as-is. This sounds backwards, but it is correct. The line above
    should be changed to something like:

    if self.gain == 1:
        chscale = self._integrationTime.scale * 16
    else:
        chscale = self._integrationTime.scale

    Near the top of p. 26 in the data sheet there is the following
    code:

    // scale if gain is NOT 16X
    if (!iGain) chScale = chScale << 4; // scale 1X to 16X

There is also a problem (possibly in my set up? python version?) that
causes the following error when the sensor saturation exception is
raised:

    line 22, in __init__
        self.msg('Channel {0} is saturated.'.format(channel))
    AttributeError: 'SensorSaturatedException' object has no attribute 'msg'

Thanks!

  • Chris Satterlee

This actually started with debugging the fact that my TSL2561 returned 0x0A in
its ID register when it was off but 0x00 when it was on.  I subsequently
realized two things: the bits are reversed from what the datasheet says, and
I can use the ID register to distinguish between the T and CS packages, so that
doesn't need to be an initialization parameter.
This was something I must have blindly copied from somewhere else.  Chris
Satterlee pointed out that it wasn't necessary, based on empirical testing.
(The datasheet backs Chris up, too.  It says that the register values are
little-endian, and the I2C's readU16() method assumes little-endian data.)
I was dividing where I should have multipled, and vice versa.  :(
I'm not sure whether this ever worked for me, but the old code certainly doesn't
work on Python 2.7 for me now.  The replacement code does what I intended the
old code to do, plus adds a channel instance variable in case that's useful to
anyone.
@asciipip
Copy link
Author

What's extra coincidental about the timing is that I wrote this module a couple of years ago and just recently decided that I ought to polish up the documentation and send a pull request.

I think I've fixed up all of the problems you pointed out. (In particular, I got the channel scaling pretty much completely backwards, which explains why 16x mode always looked weird to me. It should be right now; I get consistent results when switching between modes, at least. I don't have any other light sensors to compare my readings to, though.)

Also in here are some improvements to the use of the contents of the ID register. My original code was copied from the Arduino code (which looks a little off now; instead of if (x & 0x0A ), I think it probably at least ought to be if (x & 0x0F == 0x0A)); the new stuff is an improvement, I think.

@ladyada
Copy link
Member

ladyada commented Sep 30, 2019

Thank you for the Pull Request
This library has been deprecated in favor of our python3 Blinka library. We have replaced all of the libraries that use this repo with CircuitPython libraries that are Python3 compatible, and support a wide variety of single board/linux computers!

Visit https://circuitpython.org/blinka for more information

CircuitPython has support for almost 200 different drivers, and a as well as FT232H support for Mac/Win/Linux!

@ladyada ladyada closed this Sep 30, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants